package com.module.overview.ui.historicalDataDetails.provider

import android.graphics.Typeface
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.common.app.data.bean.overView.HistoryDetailsModel
import com.common.base.utils.MathUtil
import com.common.base.utils.SpanUtils
import com.common.base.utils.ViewUtils
import com.common.base.utils.ViewUtils.getString
import com.module.ble.data.bean.CusChartData
import com.module.ble.utils.AllCardTypeConfig
import com.module.ble.widget.HdCandlestickChart
import com.module.overview.R

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2028/08/11
 *
 * 描述：活动中高强度时长卡片
 *
 * 修订历史：
 *
 */
class OvHdActivityStrengthDurationCardProvider(
    val mType: Int = 1,
    override val itemViewType: Int = AllCardTypeConfig.TYPE_CARD_HD_ACTIVITY_INTENSITY_DURATION,
    override val layoutId: Int = R.layout.overview_hd_activity_strength_duration_crad_provider
) : BaseItemProvider<HistoryDetailsModel>() {

    private var candlestickChart: HdCandlestickChart? = null

    // 设置数据
    private val stressData = mutableListOf<CusChartData>()

    val touchValueSplicing = "${getString(R.string.pickerview_minutes)}"

    val touchValueHint = when(mType){
        3->"${getString(R.string.common_month_unit)}"
        else->"${getString(R.string.common_week_unit)}"
    }

    private var maxStages = 100f

    init {
    }

    override fun convert(helper: BaseViewHolder, item: HistoryDetailsModel) {
        candlestickChart?.showYMiddleLine(false)
        candlestickChart?.setXAxisSolidLineEnabled(true)

        // 设置自定义X轴时间标签
        var timeLabels: Map<Int, String> = mapOf()

        when(mType){
            1->{//周
                candlestickChart?.setBarSpacing(25f)
                timeLabels =  mapOf(
                    0 to "一",    // 第1条数据下方显示"一"
                    1 to "二",    // 第2条数据下方显示"二"
                    2 to "三",   // 第3条数据下方显示"三"
                    3 to "四",   // 第4条数据下方显示"四"
                    4 to "五",   // 第5条数据下方显示""五"
                    5 to "六",    // 第6条数据下方显示"五"
                    6 to "七",    // 第7条数据下方显示"七"
//            19 to "30",    // 第20条数据下方显示"30"
                )
            }
            2->{//月
                candlestickChart?.setBarSpacing(5f)
                timeLabels = mapOf(
                    0 to "一",    // 第1条数据下方显示"一"
                    1 to "二",    // 第2条数据下方显示"二"
                    2 to "三",   // 第3条数据下方显示"三"
                    3 to "四",   // 第4条数据下方显示"四"
                    4 to "五",   // 第5条数据下方显示""五"
                    5 to "六",    // 第6条数据下方显示"五"
                    6 to "七",    // 第7条数据下方显示"七"
//            19 to "30",    // 第20条数据下方显示"30"
                )
            }
            3->{//年
                candlestickChart?.setBarSpacing(5f)
                timeLabels = mapOf(
                    0 to "1",
                    1 to "2",
                    2 to "3",
                    3 to "4",
                    4 to "5",
                    5 to "6",
                    6 to "7",
                    7 to "8",
                    8 to "9",
                    9 to "10",
                    10 to "11",
                    11 to "12",
                )
            }
        }

        stressData.clear()
        // 设置动态数量的数据
        for (i in 0 until when(mType){
            2->30
            3->12
            else->7
        }) {
//            chartData.add(CusChartData(minValue = i * 2f, mValue = i * 3f))
            stressData.add(CusChartData(
                createdTime = 0,
                minValue = 0f,
                mValue = (0..8).random().toFloat(),
                dayNum = 0
            ))
        }


        maxStages = stressData.maxByOrNull { it.mValue }?.mValue?:100f

        MathUtil
        // 设置Y轴阶段
        candlestickChart?.setYAxisStages(
            floatArrayOf(0f, (maxStages/2).toInt().toFloat(), maxStages), // 阶段分界点
        )

        candlestickChart?.setBasicValue((4..7).random().toFloat())
        candlestickChart?.setData(stressData)
//        candlestickChart?.setAverageData(averageDataValues)

        // 设置自定义X轴时间标签
        candlestickChart?.setCustomTimeLabels(timeLabels)


        // 设置触摸监听
        candlestickChart?.setOnBarTouchListener(object : HdCandlestickChart.OnBarTouchListener {
            override fun onBarTouched(barIndex: Int, minValue: Float, maxValue: Float, timeLabel: String, exValue: Float) {
                // 处理触摸事件
//                Log.d("CandlestickChart", "触摸到柱子: 索引=$barIndex,触摸到: $timeLabel, 值: $minValue")

                SpanUtils.create()
                    .addSection(maxValue.toString())
                    .addForeColorSection(touchValueSplicing, ViewUtils.getColor(R.color.c_7f7f7f))
                    .setAbsSize(touchValueSplicing, 14)
                    .setStyle(touchValueSplicing, Typeface.NORMAL)
                    .showIn(helper.getView(R.id.tvTouchValue))
                helper.setText(R.id.tvTouchValueHint, when(mType){
                    2->"$timeLabel"
                    3->"$timeLabel$touchValueHint"
                    else->"$touchValueHint$timeLabel"
                })
                helper.setVisible(R.id.totalTouchValue, true)
                helper.setVisible(R.id.totalLowest, false)
                helper.setVisible(R.id.totalAverage, false)
                helper.setVisible(R.id.totalHighest, false)
            }

            override fun onTouchReleased() {
                // 处理触摸释放事件
                helper.setVisible(R.id.totalTouchValue, false)
                helper.setVisible(R.id.totalLowest, true)
                helper.setVisible(R.id.totalAverage, true)
                helper.setVisible(R.id.totalHighest, true)
            }
        })
    }


    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        candlestickChart = viewHolder.getView(R.id.candlestickChart)
        candlestickChart?.setBarRounding(enableTop = true, enableBottom = false)
        super.onViewHolderCreated(viewHolder, viewType)
    }
}